package com.oba.king.sysmanager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.swing.tree.DefaultTreeModel;

import com.icesoft.faces.context.BridgeFacesContext;
import com.oba.base.service.BaseBean;
import com.oba.base.util.TreeUtil;
import com.oba.base.util.Util;
import com.oba.king.login.UserInfo;

public class ModuleBean extends BaseBean<Module, ModuleService> {

	private DefaultTreeModel menuTree;
	private DefaultTreeModel moduleTree;
	private Map<Long, Module> moduleMap = new HashMap<Long, Module>();
	private Map<Long, Module> menuMap = new HashMap<Long, Module>();
	private Module module;
	private Module curModule;
	private boolean showAddModule;
	private String pathStr;

	@Override
	public void init() {
		module = new Module();
		showAddModule = false;
		List<Module> list = getService().getDao().findAll();
		curModule = list.get(0);
		for (Module m : list) {
			moduleMap.put(m.getId(), m);
		}
		moduleTree = TreeUtil.createTree(list);
	}

	public void addModule() {
		module = new Module();
		module.setNewBo(true);
		showAddModule = true;
	}

	public void closeAddModule() {
		showAddModule = false;
	}

	public void updateModule() {
		module = curModule;
		showAddModule = true;
	}

	public void selectModule() {
		String idStr = getParam("moduleId");
		Long id = Long.parseLong(idStr);
		curModule = moduleMap.get(id);
	}

	public void saveModule() {
		if (module.isNewBo()) {
			TreeUtil.addChrid(curModule.getNode(), module);
			module.setParent(curModule);
			moduleTree.reload();
		}
		getService().save(module);
		moduleMap.put(module.getId(), module);
		showAddModule = false;
		curModule = module;
	}

	public void deleteNodeModule() {
		moduleTree.removeNodeFromParent(curModule.getNode());
		getService().del(curModule);
	}

	public String getPath() {
		String path = (String) getSession().getAttribute(PAGE_PATH);
		System.out.println(path);
		return path;
	}

	public void goTarget() {
		Module m = menuMap.get(Long.parseLong(getParam("moduleId")));
		if (m != null) {
			pathStr = "";
			Module temp = m;
			while (temp != null) {
				pathStr = temp.getName() + pathStr;
				temp = temp.getParent();
				if (temp != null) {
					pathStr = " -> " + pathStr;
				}
			}
			String tempUrl = m.getUrl();
			if (null != tempUrl && !"".equals(tempUrl)) {
				int methodIndex = tempUrl.indexOf("method=");
				if(methodIndex < 0) {
					int len = tempUrl.indexOf("?");
					if (len > 0) {
						String url = tempUrl.substring(0, len);
						String bean = tempUrl.substring(len+1);
						BridgeFacesContext context = (BridgeFacesContext) FacesContext.getCurrentInstance();
						Util.beanInit(context, bean + "&method=init");
						goTarget(url);
					} else {
						goTarget(tempUrl);
					}
				} else {
					int len = tempUrl.indexOf("?");
					if (len > 0) {
						String url = tempUrl.substring(0, len);
						String bean = tempUrl.substring(len+1);
						BridgeFacesContext context = (BridgeFacesContext) FacesContext.getCurrentInstance();
						Util.beanInit(context, bean);
						goTarget(url);
					} else {
						goTarget(tempUrl);
					}
				}
			}
		}
	}

	public void buildMenuTree(UserInfo user) {
		// List<Module> menuList =
		// getService().getDao().findModuleByGroup(user.getUserGroup());
		List<Module> menuList = getService().getDao().findAll();
		for (Module m : menuList) {
			menuMap.put(m.getId(), m);
		}
		menuTree = TreeUtil.createTree(menuList);
	}

	public void goTest2() {
		System.out.println("sdfsafda");
		goTarget("/public/test2.jspx");
	}

	@Override
	public String doFind() {
		// TODO Auto-generated method stub
		return null;
	}

	public DefaultTreeModel getMenuTree() {
		return menuTree;
	}

	public void setMenuTree(DefaultTreeModel menuTree) {
		this.menuTree = menuTree;
	}

	public DefaultTreeModel getModuleTree() {
		if (moduleTree == null) {
			init();
		}
		return moduleTree;
	}

	public void setModuleTree(DefaultTreeModel moduleTree) {
		this.moduleTree = moduleTree;
	}

	public Module getModule() {
		return module;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public boolean isShowAddModule() {
		return showAddModule;
	}

	public void setShowAddModule(boolean showAddModule) {
		this.showAddModule = showAddModule;
	}

	public Module getCurModule() {
		return curModule;
	}

	public void setCurModule(Module curModule) {
		this.curModule = curModule;
	}

	public String getPathStr() {
		return pathStr;
	}

}
